<?php

function dumpKey($pk)
{
    if (count($pk) == 0) return ': none';
    return '('.count($pk).'): '.implode(', ', $pk);
}
function dumpTable(\WDB\Analyzer\TableView $table)
{
    $type = ($table instanceof WDB\Analyzer\Table) ? 'Table' : 'View';
    echo '<h2>'.$type.': '.$table->name.'</h2>';
    echo 'Primary key'.dumpKey($table->primaryKey)."<br>";
    echo 'Unique keys ('.count($table->uniqueKeys).'): <br>';
    foreach ($table->uniqueKeys as $name=>$key)
    {
        echo "$name".dumpKey($key)."<br>";
    }
    if ($table instanceof \WDB\Analyzer\Table)
    {
        if (count($table->foreignKeys) > 0)
        {
            echo 'Foreign keys ('.count($table->foreignKeys).'): <br>';
            foreach ($table->foreignKeys as $name=>$key)
            {
                echo "$name (".implode(',', $key->columns).") -> {$key->refSchema}.{$key->refTable}(".implode(',', $key->refColumns).")<br>";
            }
        }
        else
        {
            echo 'no foreign keys';
        }
    }
    echo '<table><tr><th>Name</th><th>Type</th><th>Meta<th></tr>';
    foreach ($table->columns as $column)
    {
        echo '<tr><td>'.htmlspecialchars($column->name).'</td><td>'.htmlspecialchars(get_class($column)).'</td><td style="white-space:pre">';
        echo '</td></tr>';
    }
    echo '</table>';
}

require '../nette.min.php';
Nette\Diagnostics\Debugger::enable();
require '../loader.php';
$schema = Database::getInstance()->getSchema();
?>
<html><head>
<style type="text/css">
    td {border:1px solid black;vertical-align:top;}
</style>
    </head><body>
<?php
echo '<h1>Schema: '.htmlspecialchars($schema->name).'</h1>';
foreach ($schema->tables as $table) dumpTable($table);
?>
    </body></html>